//
//  EmptyModifer.swift
//  DocScaner
//
//  Created by liyahao on 2025/8/18.
//

import SwiftUI

struct EmptyModifer: ViewModifier {
    @Binding var show: Bool
    @Binding var reLoading: Bool
    @State var shake = false
    var showBtn: Bool
    var title: String
    
    func body(content: Content) -> some View {
        content
            .overlay {
                if show {
                    if showBtn {
                        Button { reLoading.toggle() } label: {
                            VStack(spacing: 10) {
                                Image("scaner_empty")
                                    .resizable()
                                    .scaledToFit()
                                    .frame(width: 130, height: 130)
                                    .offset(x: shake ? -10 : 0)
                                    .animation(.default.repeatCount(5).speed(5), value: shake)
                                    .onAppear {
                                        DispatchQueue.main.asyncAfter(deadline: .now() + 0.3, execute: {
                                            self.shake = true
                                        })
                                    }
                                Text(title)
                                    .font(.system(size: 14, weight: .regular))
                                    .foregroundColor(.secondary)
                            }
                        }
                    } else {
                        Image("scaner_empty")
                            .resizable()
                            .scaledToFit()
                            .frame(width: 130, height: 130)
                            .offset(x: shake ? -10 : 0)
                            .animation(.default.repeatCount(5).speed(5), value: shake)
                            .onAppear {
                                DispatchQueue.main.asyncAfter(deadline: .now() + 0.3, execute: {
                                    self.shake = true
                                })
                            }
                    }
                }
            }
    }
}

extension View {
    func emptyData(_ isEmpty: Binding<Bool>, _ reLoading: Binding<Bool> = .constant(true), showBtn: Bool = true, title: String = "刷新") -> some View {
        return self.modifier(EmptyModifer(show: isEmpty, reLoading: reLoading, showBtn: showBtn, title: title))
    }
}
